package com.vitogiovanni.tobook.utilities;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.vitogiovanni.tobook.R;

public class LocaliListViewAdapter extends ArrayAdapter<LocaleItem>{
	
	Context context;
	
	public LocaliListViewAdapter(Context context, int resource,
			List<LocaleItem> objects) {
		super(context, resource, objects);
		this.context = context;
	}
	
	private class ViewHolder {
		TextView title;
        TextView tipologia;
        TextView viaCitta;
        ImageView avatarLocale;
        TextView distanzaLocale;
    }
	
	public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        LocaleItem localeItem = getItem(position);
 
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_items_locali, null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.nome_locale);
            holder.tipologia = (TextView) convertView.findViewById(R.id.tipologia_locale);
            holder.viaCitta = (TextView) convertView.findViewById(R.id.viaCitta_locale);
            holder.avatarLocale = (ImageView) convertView.findViewById(R.id.avatar_locale);
            holder.distanzaLocale = (TextView) convertView.findViewById(R.id.distanza_locale);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
 
        holder.title.setText(localeItem.getNome());
        holder.tipologia.setText(localeItem.getTipologia());
        holder.viaCitta.setText(localeItem.getVia() + ", " + localeItem.getCitta());
        holder.avatarLocale.setImageBitmap(localeItem.getFoto());
        String distanza = localeItem.getDistanza();
        if(!distanza.equals("--"))
        {
        	distanza = distanza.concat(" km");
        }	
        holder.distanzaLocale.setText(distanza);
        return convertView;
    }
	
}
